package edu.gatech.cs2340.sharks.fms;

/**
 * FilterSearchActivity.java
 * @author Steven Mitchell
 * 
 * This activity allows users to search the database for items.
 * Results are displayed in a separate activity, ShowFilteredResultsActivity.java
 */

import java.util.List;
import edu.gatech.cs2340.sharks.fms.data.*;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.RadioButton;

public class FilterSearchActivity extends Activity
{
	/**
	 * An int representing the item status (so that the data
	 * may be more easily passed to ShowFilteredResultsActivity.java).
	 * Is changed to a 1 or a 2 depending on which radio button the
	 * user has selected.
	 */
	private int itemStatus = 0;
	
	/**
	 * The current activity. Defaults to null, but is updated if the activity
	 * successfully starts.
	 */
	private static Activity instance = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_filter_search);
		if (instance != null) instance.finish();
		instance = this;
	}
	
	/**
	 * Queries the database for the list of possible tags and
	 * concatenates them into a string. Afterward, it adds the
	 * string to a dialog that is displayed to the user.
	 * 
	 * @param view The button that runs the method when clicked.
	 */
	public void displayTags(View view)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		
		String tagString = "";
		Packet<List<Tag>> tagList = DBMgr.allTags();

		for (Tag t : tagList.value)
		{
			if ("".equals(tagString)) tagString += t.name;
			else tagString += ", " + t.name;
		}
		
		builder.setMessage("Here are the tags you may use: \n" + tagString);
		builder.setPositiveButton("Close", new DialogInterface.OnClickListener()
		{	
			public void onClick(DialogInterface dialog, int id) { }
		});
		
		AlertDialog dialog = builder.create();
		dialog.show();
	}
	
	/**
	 * Sets lost to true or false based on which radiobutton the user has selected
	 * 
	 * @param view The button that is checked.
	 */
	public void setLostOrFound(View view)
	{
		boolean checked = ((RadioButton) view).isChecked();
		switch(view.getId())
		{
			case R.id.editLost:
			    if (checked) itemStatus = 1;
			    break;
			case R.id.editFound:
			    if (checked) itemStatus = 2;
			    break;
		}
	}
	
	/**
	 * Obtains all of the search paramters and passes them to the Show Filtered Search Results activity.
	 * 
	 * @param view The button that runs this method when clicked.
	 */
	public void filteredSearch(View view)
	{
		//obtains the name of the item
		final EditText itemNameField = (EditText) findViewById(R.id.itemName);
		String itemName = itemNameField.getText().toString();
		
		//obtains the location of the item
		final EditText itemLocationField = (EditText) findViewById(R.id.location);
		String location = itemLocationField.getText().toString();
		
		//obtains the start year, month, and day and concatenates them
		final EditText startYearField = (EditText) findViewById(R.id.startYear);
		String startYear = startYearField.getText().toString();
		
		final EditText startMonthField = (EditText) findViewById(R.id.startMonth);
		String startMonth = startMonthField.getText().toString();
		
		final EditText startDayField = (EditText) findViewById(R.id.startDay);
		String startDay = startDayField.getText().toString();
		
		String startDate = startYear + "-" + startMonth + "-" + startDay;
		
		//obtains the end year, month, and day and concatenates them
		final EditText endYearField = (EditText) findViewById(R.id.endYear);
		String endYear = endYearField.getText().toString();
		
		final EditText endMonthField = (EditText) findViewById(R.id.endMonth);
		String endMonth = endMonthField.getText().toString();
		
		final EditText endDayField = (EditText) findViewById(R.id.endDay);
		String endDay = endDayField.getText().toString();
		
		String endDate = endYear + "-" + endMonth + "-" + endDay;
		
		//obtains the list of tags and splits it
		final EditText tagField = (EditText) findViewById(R.id.tagList);
		String tagString = tagField.getText().toString();
		
		//passes all of the above parameters to ShowFilteredResultsActivity.java
		Intent next = new Intent(this, ShowFilteredResultsActivity.class);
		next.putExtra("itemName", itemName);
		next.putExtra("location", location);
		next.putExtra("startDate", startDate);
		next.putExtra("endDate", endDate);
		next.putExtra("itemBinary", itemStatus);
		next.putExtra("tagString", tagString);
		startActivity(next);
	}
	
	/**
	 * Finishes the activity and returns the user to the home screen.
	 * 
	 * @param view The button that runs this method when clicked.
	 */
	public void cancel(View view)
	{
		finish();
	}
}